// CadToolSelect.cpp
//
#include "CadToolSelect.h"

#include "CadEngine.h"

#include "OpenGL/Box.h"
#include "OpenGL/OpenGL.h"
#include "OpenGL/Renderer.h"

//-------------------------------------------------------------------------------------------------
CadToolSelect::CadToolSelect()
: CadTool("Selection",2)
{}

//-------------------------------------------------------------------------------------------------
CadToolSelect::~CadToolSelect()
{}

//-------------------------------------------------------------------------------------------------
void CadToolSelect::execute()
{
	CadEngine::Command c;

	if( _nPoints == _totPoints )
	{
		c.command   = CadEngine::Select;
		c.primitive = CadEngine::Quad;
		c.points.push_back( _points[0] );
		c.points.push_back( _points[1] );
		c.attributes += "Name:SelectArea;";
	}
	else
	{
		c.command   = CadEngine::Select;
		c.primitive = CadEngine::Point;
		c.points.push_back( _points[0] );
		c.points.push_back( _points[1] );
		c.attributes += "Name:SelectPoint;";		
	}

	_manager->issueCommand(c);
}

//-------------------------------------------------------------------------------------------------
void CadToolSelect::draw( OpenGL::Renderer *r )
{
	if(_nPoints < 2) return;

	OpenGL::Math::box3 box;
	box.extend( OpenGL::Math::dvec3(_points[0].x,_points[0].y,0)-r->origin() );
	box.extend( OpenGL::Math::dvec3(_points[1].x,_points[1].y,0)-r->origin() );

	glColor4ub(0xFF, 0xFF, 0xFF,0x50);
	glBegin( GL_TRIANGLE_STRIP );
		glVertex2d( box.minPoint().x, box.maxPoint().y );
		glVertex2d( box.minPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.maxPoint().y );
		glVertex2d( box.maxPoint().x, box.minPoint().y );
	glEnd();

	glLineWidth(.5f);	
	glColor4ub(0x00, 0x00, 0x00,0xFF);
	glBegin( GL_LINE_LOOP);
		glVertex2d( box.minPoint().x, box.maxPoint().y );
		glVertex2d( box.minPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.minPoint().y );
		glVertex2d( box.maxPoint().x, box.maxPoint().y );
	glEnd();
}